package com.fancymusic.controller;


import com.fancymusic.entity.User;
import com.fancymusic.service.IUserService;
import com.fancymusic.utils.QiniuUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private IUserService userService;

    @Autowired
    QiniuUtils qiniuUtils;

    @GetMapping("/findAllUser")
    public List<User> findAllUser(){
        return userService.findUserList();
    }

    @GetMapping("/findUserListByPage/{page}/{size}")
    public PageInfo<User> findUserListByPage(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size){
        return userService.findUserListByPage(page,size);
    }

    @PostMapping("/insertUser")
    public String insertUser(@RequestBody User user){
        Boolean isInsert = userService.insertUser(user);
        return isInsert?"success":"fail";
    }

    @GetMapping("/findByUserId/{userId}")
    public User findByUserId(@PathVariable("userId")Integer userId){
        User byUserId = userService.findByUserId(userId);
        return byUserId;
    }

    @PostMapping("/updateUser")
    public String updateUser(@RequestBody User user){
        Boolean updateUser = userService.updateUser(user);
        return updateUser?"success":"fail";
    }

    @PostMapping(value = "/avaterUpload")
    public String avaterUpload(@RequestParam("file") MultipartFile multipartFile) {

        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println(originalFilename);
        return qiniuUtils.upload(multipartFile);
    }


}
